﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GLYFrom.DiCi
{
    public partial class CaiPin : Form
    {
        public CaiPin()
        {
            InitializeComponent();
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {

        }

        private void button9_Click(object sender, EventArgs e)
        {
            DiCi.ToCn from = new ToCn();
            from.Show();
        }

        private void button8_Click(object sender, EventArgs e)
        {
            DiCi.GoWuChe from = new GoWuChe();
            from.Show();
        }

        private void button10_Click(object sender, EventArgs e)
        {
            DiCi.ChongZhi from = new ChongZhi();
            from.Show();
        }

        private void CaiPin_Load(object sender, EventArgs e)
        {
            //加载表
            this.dataGridView1.AutoGenerateColumns = false;
            DataTable table = DBHelper.select("select * from T_CaiPin");
            this.dataGridView1.DataSource = table;

            //显示余额
            DataTable Table = DBHelper.select(string.Format("select * from T_YongHu where YHId='{0}'",GeRenXX.Id)); 
            int money=Convert.ToInt32(Table.Rows[0]["YuE"]);
            this.label_YuE.Text = money.ToString();

        }

        private void button3_Click(object sender, EventArgs e)
        {
            //刷新
            DataTable table = DBHelper.select("select * from T_CaiPin");
            this.dataGridView1.DataSource = table;
            DataTable Table = DBHelper.select(string.Format("select * from T_YongHu where YHId='{0}'", GeRenXX.Id));
            int money = Convert.ToInt32(Table.Rows[0]["YuE"]);
            this.label_YuE.Text = money.ToString();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //清空输入内容
            this.text_Name.Text = "";
        }

        private void button1_Click(object sender, EventArgs e)
        {

            string Name = this.text_Name.Text.Trim();
            DataTable table = DBHelper.select(string.Format("select * from T_CaiPin where Name like '%{0}%'",Name));
            this.dataGridView1.DataSource = null;
            this.dataGridView1.DataSource = table;
        }

        private void 加入购物车ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //用户Id
            int Id = GeRenXX.Id;
            //下单时间
            DateTime time = DateTime.Now;
            //订单号
            DateTime Time = time.AddDays(GeRenXX.Id);
            string DDH = Time.ToString("yyyyMMddhhmmssfff");
            DDH += string.Format("{0}",Id);
            //商品名称
            string Name = this.dataGridView1.SelectedRows[0].Cells["Col_Name"].Value.ToString();
            //价格
            int Money = Convert.ToInt32(this.dataGridView1.SelectedRows[0].Cells["Col_JG"].Value);
            //订单状态
            int ZT = Convert.ToInt32(MeiJu.JR);
            int sr = DBHelper.ZSG(string.Format("insert into T_DingDan values('{0}','{1}','{2}','{3}','{4}','{5}')", Id, DDH, Name, time, Money, ZT));
            if (sr>0)
            {
                MessageBox.Show("已成功加入购物车!");
            }
            else
            {
                MessageBox.Show("加入失败...");
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void pictureBox5_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void pictureBox3_Click(object sender, EventArgs e)
        {
            this.text_Name.Text = "";
        }
    }
}
